package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHUtil
import cn.com.kh.signUtil
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.alibaba.fastjson.parser.Feature
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 审核人员查询接口
 */

public class apiGetAuditorList implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiGetAuditorList.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        // 初始化返回参数
        JSONObject result = new JSONObject();

        JSONObject resultJson = null;
        //接收客户端调用参数
        String callJson = "";
        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }

        log.info("[调用请求信息]:" + callJson);

        JSONObject signJson = signUtil.CheckSign(JSONObject.parseObject(callJson, Feature.OrderedField));
        log.info("[签名验证结果]:");
        log.info(signJson);
        if (signJson != null && 1==2) {
            return signJson.toString();
        } else {
            try {
                //解析Json
                JSONObject inJson = JSONObject.parseObject(callJson, Feature.OrderedField);

                //获取查询参数
                String c_staff_id = inJson.getString("staff_id");
                String c_rolegroup_id = inJson.getString("rolegroup_id");

                log.debug("请求参数-查询条件-c_staff_id:" + c_staff_id + " c_rolegroup_id:" + c_rolegroup_id);

                String querySQL = '''Select R1.Staff_Id As staff_id, R2.Staffname As staff_name
                                      From Staffrole R1
                                      Left Join Staff R2
                                        On R1.Staff_Id = R2.Staff_Id
                                     Where R1.Role_Id = 'MAIL01\'
                                       And R2.Workstatus = 'ONLINE\'''';

                List queryList = eliteBean.chaxun_sql(querySQL, "dataSource");

                //获取总行数
                log.debug("执行查询数据SQL获取总条数:" + queryList.size());

                String reJoson = Util.getJsonFromList(queryList);
                reJoson=!KHUtil.isNull(reJoson)?reJoson:"[]";

                long endTime = System.currentTimeMillis();
                result.put("code", 1);
                result.put("msg", "处理完成、调用问题处理时长(毫秒):" + (endTime - startTime));
                JSONArray dataArr = JSONArray.parseArray(reJoson);
                result.put("data", dataArr);

            } catch (Exception e) {
                result.put("code", 1008);
                result.put("msg", e.getMessage());
            }

        }

        return result.toString();
    }
}